﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;

namespace Besture.UI.Admin.Common
{
    public partial class ChooseFacilityTree : System.Web.UI.Page
    {
        public string _json = string.Empty;

        public List<string> listPermission = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable table = new HotelsCategoryDac().GetDataTableByCategoryType(Constants.HOTEL_CATEGORY_FACILITY.ToString()); ;


            listPermission = new List<string>();
            string userPermissions = StringHelper.Query("facilityValues");
            listPermission = StringHelper.SplitStringToList(userPermissions);

            _json = ZtreeJson(table);

        }

        #region 绑定Json数据
        /// <summary>
        /// 绑定Json数据
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public string ZtreeJson(DataTable table)
        {
            StringBuilder stringBuilder = new StringBuilder();

            QueryDac dac = new QueryDac();

            stringBuilder.Append("{id:-1, pId:-1, name: '所有设施', open:true},");

            foreach (DataRow row in table.Rows)
            {
                string modulesId = row["ID"].ToString();

                if (listPermission.Count > 0)
                {
                    string checkEdStr = "";
                    if (listPermission.Contains(modulesId))
                    {
                        checkEdStr = ",checked:true";
                    }
                    stringBuilder.Append("{id:" + modulesId + ", pId:-1, name: '" + row["CategoryName"] + "', open:true" + checkEdStr + "},");
                }
                else
                {
                    stringBuilder.Append("{id:" + row["ID"] + ", pId:-1, name: '" + row["CategoryName"] + "', open:true},");
                }
            }
            string json = string.Format(@"[{0}];", stringBuilder.ToString().TrimEnd(','));
            return json;
        }
        #endregion
        
    }
}